package com.carb.controller.admin;

import com.carb.annotation.RequiredRoles;
import com.carb.constant.AuthorityConstant;
import com.carb.dto.AnnouncementAddDTO;
import com.carb.dto.AnnouncementUpdateDTO;
import com.carb.entity.Announcement;
import com.carb.result.Result;
import com.carb.service.IAnnouncementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 公告管理 前端控制器
 * </p>
 *
 * @author XuChaoyang
 * @since 2025-01-18
 */
@RestController("adminAnnouncementController")
@RequestMapping("/admin/announcement")
@Tag(name = "公告管理相关接口")
@Slf4j
public class AnnouncementController {

    @Resource
    private IAnnouncementService announcementService;

    /**
     * 新增公告
     * @param announcementAddDTO 新增公告数据模型
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER})
    @PostMapping("/add")
    @Operation(summary = "新增公告")
    public Result<String> addAnnouncement(@RequestBody AnnouncementAddDTO announcementAddDTO) {
        log.info("新增公告：{}", announcementAddDTO);
        announcementService.addAnnouncement(announcementAddDTO);
        log.info("新增公告成功！");
        return Result.success();
    }

    /**
     * 查询所有公告
     * @return 公告信息列表
     */
    @GetMapping("/list")
    @Operation(summary = "查询所有公告")
    public Result<List<Announcement>> listAll() {
        log.info("查询所有公告");
        List<Announcement> announcementList = announcementService.listAll();
        log.info("查询所有公告成功！");
        return Result.success(announcementList);
    }

    /**
     * 根据公告id查询公告信息
     * @param id 公告id
     * @return 查询结果
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据公告id查询公告信息")
    public Result<Announcement> getAnnouncementById(@PathVariable @Parameter(description = "公告id") Long id) {
        log.info("根据公告id查询公告信息，公告id：{}", id);
        Announcement announcement = announcementService.getAnnouncementById(id);
        log.info("根据公告id查询公告信息成功！");
        return Result.success(announcement);
    }

    /**
     * 修改公告信息
     * @param announcementUpdateDTO 修改的公告信息
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER})
    @PutMapping
    @Operation(summary = "修改公告信息")
    public Result<String> updateAnnouncement(@RequestBody AnnouncementUpdateDTO announcementUpdateDTO) {
        log.info("修改公告信息：{}", announcementUpdateDTO);
        announcementService.updateAnnouncement(announcementUpdateDTO);
        log.info("修改公告信息成功！");
        return Result.success();
    }

    /**
     * 删除公告
     * @param id 公告id
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER})
    @DeleteMapping("/{id}")
    @Operation(summary = "删除公告")
    public Result<String> deleteAnnouncement(@PathVariable @Parameter(description = "公告id") Long id) {
        log.info("删除公告，公告id：{}", id);
        announcementService.deleteAnnouncement(id);
        log.info("删除公告成功！");
        return Result.success();
    }

}
